<?php
namespace app\common\controller;

/**
 * 事项类
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/08
 * Time: 11:41
 */
class Matter{

    /**
     * 生成事项（未开始）
     * @param $from_openid
     * @param $sourceid
     * @param $source
     * @param $out_time
     * @param $deal_openid
     * @param $confirm_status
     * @return int
     */
    public function gen_matter_not($from_openid,$sourceid,$source,$out_time,$deal_openid,$confirm_status){
        $name = $this->deal_matter_name($sourceid,$source);

        //生成事项
        $maps = [];
        $maps[] = ['from_openid','=',$from_openid];
        $maps[] = ['sourceid','=',$sourceid];
        $maps[] = ['source','=',$source];
        $maps[] = ['status','=',0];
        $m_detail = model('matter')->get_detail($maps,'id');
        if(empty($m_detail)){
            $m_data = [];
            $m_data['from_openid'] = $from_openid;
            $m_data['sourceid'] = $sourceid;
            $m_data['source'] = $source;
            $m_data['name'] = $name;
            $m_data['out_time'] = $out_time;
            $m_data['deal_openid'] = $deal_openid;
            $m_data['confirm_status'] = $confirm_status;
            $r = model('matter')->insert($m_data);
        }else{
            $m_data = [];
            $m_data['out_time'] = $out_time;
            $m_data['deal_openid'] = $deal_openid;

            $maps = [];
            $maps[] = ['id','=',$m_detail['id']];
            $r = model('matter')->where($maps)->update($m_data);
        }
        return 1;
    }

    /**
     * 修改事项（打开）
     * @param $from_openid
     * @param $sourceid
     * @param $source
     * @return int
     */
    public function save_matter_open($from_openid,$sourceid,$source){
        $maps = [];
        $maps[] = ['from_openid','=',$from_openid];
        $maps[] = ['sourceid','=',$sourceid];
        $maps[] = ['source','=',$source];
        $m_detail = model('matter')->get_detail($maps,'id');
        if(!empty($m_detail)){
            $m_data = [];
            $m_data['status'] = 2;

            $maps = [];
            $maps[] = ['id','=',$m_detail['id']];
            $r = model('matter')->force()->save($m_data,$maps);

            //更新创建事项积分
            $si = new SaveIntegral();
            $si->save_cm_integral($m_detail['id']);

            //发送消息
            $r = $this->send_matter_msg($m_detail['id'],0,'感谢您的协作！！！');
        }
        return 1;
    }

    /**
     * 处理事项名称
     * @param $sourceid
     * @param $source 1：巡店；2：内部协同
     * @return int|string
     */
    public function deal_matter_name($sourceid,$source){
        $name = '';
        switch($source){
            case 1:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $crm_detail = model('cp_region_modular')->get_detail($maps,'id,branchid,floorid,regionid,modularid,action');

                //查找分部
                $maps = [];
                $maps[] = ['id','=',$crm_detail['branchid']];
                $b_detail = model('branch')->get_detail($maps,'id,name');

                //查找楼层
                $maps = [];
                $maps[] = ['id','=',$crm_detail['floorid']];
                $f_detail = model('floor')->get_detail($maps,'id,name');

                //查找区域
                $maps = [];
                $maps[] = ['id','=',$crm_detail['regionid']];
                $r_detail = model('region')->get_detail($maps,'id,name');

                //查找模块
                $maps = [];
                $maps[] = ['id','=',$crm_detail['modularid']];
                $m_detail = model('modular')->get_detail($maps,'id,name');

                $name = '巡店-' . $b_detail['name'] . '-' . $f_detail['name'] . '-' . $r_detail['name'] . '-' . $m_detail['name'];
                break;
            case 2:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wrx_detail = model('work_record_xt')->get_detail($maps,'id,name');
                $name = '内部协同-' . $wrx_detail['name'];
                break;
            default:
                return 0;
        }
        return $name;
    }

    /**
     * 获取事项处理动态
     * @param $matterid
     * @return int
     * $m_detail['source'] 1：巡店；2：内部协同
     */
    public function get_matter_deal($matterid){
        $deal_info = [];
        $maps = [];
        $maps[] = ['id','=',$matterid];
        $m_detail = model('matter')->get_detail($maps,'id,from_openid,sourceid,source,out_time,deal_openid,c_time');
        switch($m_detail['source']){
            case 1:
                //查找发起人用户
                $maps = [];
                $maps[] = ['openid','=',$m_detail['from_openid']];
                $from_u_detail = model('user')->get_detail($maps,'id,username');

                //查找巡店内容，第一个条处理动态开始
                $maps = [];
                $maps[] = ['id','=',$m_detail['sourceid']];
                $crm_detail = model('cp_region_modular')->get_detail($maps,'id,pic,explain');
                $crm_detail['pic'] = !empty($crm_detail['pic']) ? json_decode($crm_detail['pic'],true) : [];

//                if($m_detail['out_time'] != '0000-00-00 00:00:00'){
//                    $deal_str = '【' . $from_u_detail['username'] . '】稍后处理，截止时间：' . date('m-d H:i',strtotime($m_detail['out_time']));
//                }else{
//                    $maps = [];
//                    $maps[] = ['matterid','=',$m_detail['id']];
//                    $mf_detail = model('matter_flow')->get_detail($maps,'id,user_openid');
//
//                    $deal_openid = !empty($mf_detail) ? $mf_detail['user_openid'] : $m_detail['deal_openid'];
//
//                    //查找处理人用户
//                    $maps = [];
//                    $maps[] = ['openid','=',$deal_openid];
//                    $deal_u_detail = model('user')->get_detail($maps,'id,username');
//
//                    $deal_str = '【' . $from_u_detail['username'] . '】移交事项给' . '【' . $deal_u_detail['username'] . '】';
//                }

                $deal_str = '【' . $from_u_detail['username'] . '】创建该事项';

                $from_arr['id'] = 0;
                $from_arr['deal_str'] = $deal_str;
                $from_arr['c_time'] = date('m-d H:i',strtotime($m_detail['c_time']));
                $from_arr['pic'] = $crm_detail['pic'];
                $from_arr['explain'] = $crm_detail['explain'];
                //查找巡店内容，第一个条处理动态结束

                $maps = [];
                $maps[] = ['matterid','=',$m_detail['id']];
                $mf_list = model('matter_flow')->get_list($maps,'*','id desc');
                $deal_info = [];
                foreach($mf_list as $k => $v){
                    //查找处理人用户
                    $maps = [];
                    $maps[] = ['openid','=',$v['user_openid']];
                    $deal_u_detail = model('user')->get_detail($maps,'id,username');

                    //查找动作
                    $maps = [];
                    $maps[] = ['action','=',$v['action']];
                    $mad_detail = model('matter_action_dict')->get_detail($maps,'id,action,name');

                    if($mad_detail['action'] == 104){
                        $maps = [];
                        $maps[] = ['id','>',$v['id']];
                        $maps[] = ['matterid','=',$v['matterid']];
                        $mf_detail = model('matter_flow')->get_detail($maps,'id,user_openid');

                        $deal_openid = !empty($mf_detail) ? $mf_detail['user_openid'] : $m_detail['deal_openid'];

                        //查找下一个处理人用户
                        $maps = [];
                        $maps[] = ['openid','=',$deal_openid];
                        $next_deal_u_detail = model('user')->get_detail($maps,'id,username');
                        $deal_str = '【' . $deal_u_detail['username'] . '】' . $mad_detail['name'] . '事项给' . '【' . $next_deal_u_detail['username'] . '】';
                    }else{
                        $deal_str = '【' . $deal_u_detail['username'] . '】' . $mad_detail['name'] . '该事项';
                    }

                    $deal_info[$k]['id'] = $v['id'];
                    $deal_info[$k]['deal_str'] = $deal_str;
                    $deal_info[$k]['c_time'] = date('m-d H:i',strtotime($v['c_time']));
                    $deal_info[$k]['pic'] = !empty($v['pic']) ? json_decode($v['pic'],true) : [];
                    $deal_info[$k]['explain'] = $v['explain'];
                }
                array_push($deal_info,$from_arr);
                break;
            case 2:
                //查找发起人用户
                $maps = [];
                $maps[] = ['openid','=',$m_detail['from_openid']];
                $from_u_detail = model('user')->get_detail($maps,'id,username');

                //查找内部协同内容，第一个条处理动态开始
                $maps = [];
                $maps[] = ['id','=',$m_detail['sourceid']];
                $wrx_detail = model('work_record_xt')->get_detail($maps,'id,content,pic');
                $wrx_detail['pic'] = !empty($wrx_detail['pic']) ? json_decode($wrx_detail['pic'],true) : [];

//                $maps = [];
//                $maps[] = ['matterid','=',$m_detail['id']];
//                $mf_detail = model('matter_flow')->get_detail($maps,'id,user_openid');
//
//                $deal_openid = !empty($mf_detail) ? $mf_detail['user_openid'] : $m_detail['deal_openid'];
//
//                //查找处理人用户
//                $maps = [];
//                $maps[] = ['openid','=',$deal_openid];
//                $deal_u_detail = model('user')->get_detail($maps,'id,username');
//
//                $deal_str = '【' . $from_u_detail['username'] . '】移交事项给' . '【' . $deal_u_detail['username'] . '】';

                $deal_str = '【' . $from_u_detail['username'] . '】创建该事项';

                $from_arr['id'] = 0;
                $from_arr['deal_str'] = $deal_str;
                $from_arr['c_time'] = date('m-d H:i',strtotime($m_detail['c_time']));
                $from_arr['pic'] = $wrx_detail['pic'];
                $from_arr['explain'] = $wrx_detail['content'];
                //查找内部协同内容，第一个条处理动态结束

                $maps = [];
                $maps[] = ['matterid','=',$m_detail['id']];
                $mf_list = model('matter_flow')->get_list($maps,'*','id desc');
                $deal_info = [];
                foreach($mf_list as $k => $v){
                    //查找处理人用户
                    $maps = [];
                    $maps[] = ['openid','=',$v['user_openid']];
                    $deal_u_detail = model('user')->get_detail($maps,'id,username');

                    //查找动作
                    $maps = [];
                    $maps[] = ['action','=',$v['action']];
                    $mad_detail = model('matter_action_dict')->get_detail($maps,'id,action,name');

                    if($mad_detail['action'] == 104){
                        $maps = [];
                        $maps[] = ['id','>',$v['id']];
                        $maps[] = ['matterid','=',$v['matterid']];
                        $mf_detail = model('matter_flow')->get_detail($maps,'id,user_openid');

                        $deal_openid = !empty($mf_detail) ? $mf_detail['user_openid'] : $m_detail['deal_openid'];

                        //查找下一个处理人用户
                        $maps = [];
                        $maps[] = ['openid','=',$deal_openid];
                        $next_deal_u_detail = model('user')->get_detail($maps,'id,username');
                        $deal_str = '【' . $deal_u_detail['username'] . '】' . $mad_detail['name'] . '事项给' . '【' . $next_deal_u_detail['username'] . '】';
                    }else{
                        $deal_str = '【' . $deal_u_detail['username'] . '】' . $mad_detail['name'] . '该事项';
                    }

                    $deal_info[$k]['id'] = $v['id'];
                    $deal_info[$k]['deal_str'] = $deal_str;
                    $deal_info[$k]['c_time'] = date('m-d H:i',strtotime($v['c_time']));
                    $deal_info[$k]['pic'] = !empty($v['pic']) ? json_decode($v['pic'],true) : [];
                    $deal_info[$k]['explain'] = $v['explain'];
                }
                array_push($deal_info,$from_arr);
                break;
        }

        $data['deal_info'] = $deal_info;
        return $data;
    }

    /**
     * 获取事项处理动作
     * @param $openid
     * @param $matterid
     * @return int
     * $m_detail['source'] 1：巡店；2：内部协同；
     */
    public function get_matter_action($openid,$matterid){
        $action_info = [];

        $maps = [];
        $maps[] = ['id','=',$matterid];
        $m_detail = model('matter')->get_detail($maps,'id,from_openid,deal_openid,sourceid,source,out_time,status,c_time');
        switch($m_detail['source']){
            case 1:
                if($m_detail['from_openid'] == $openid && $m_detail['deal_openid'] == $openid){
                    $mac_maps = [];
                    //$mac_maps[] = ['action','in',[101,104,105]];
                    $mac_maps[] = ['action','in',[101,103,104]];
                }elseif($m_detail['deal_openid'] == $openid){
                    $mac_maps = [];
                    $mac_maps[] = ['action','in',[102,103,104]];
                }else{
                    $mac_maps[] = ['action','=',''];
                }
                $mad_list = model('matter_action_dict')->get_list($mac_maps,'action,name');
                $action_info = $mad_list;
                break;
            case 2:
                if($m_detail['from_openid'] == $openid && $m_detail['deal_openid'] == $openid){
                    $mac_maps = [];
                    //$mac_maps[] = ['action','in',[101,104,105]];
                    $mac_maps[] = ['action','in',[101,103,104]];
                }elseif($m_detail['deal_openid'] == $openid){
                    $mac_maps = [];
                    $mac_maps[] = ['action','in',[102,103,104]];
                }else{
                    $mac_maps[] = ['action','=',''];
                }
                $mad_list = model('matter_action_dict')->get_list($mac_maps,'action,name');
                $action_info = $mad_list;
                break;
        }

        $data['action_info'] = $action_info;
        return $data;
    }

    /**
     * 生成事项流程
     * @param $user_openid
     * @param $matterid
     * @param $action
     * @param $out_time
     * @param $deal_openid
     * @param $explain
     * @param $pic
     * @return int
     */
    public function gen_matter_flow($user_openid,$matterid,$action,$out_time,$deal_openid,$explain,$pic){
        $mf_data = [];
        $mf_data['matterid'] = $matterid;
        $mf_data['user_openid'] = $user_openid;
        $mf_data['action'] = $action;
        $mf_data['explain'] = trim($explain);
        $mf_data['pic'] = json_encode($pic,JSON_UNESCAPED_UNICODE);
        $r = model('matter_flow')->insert($mf_data);

        switch($action){
            case 101:
                $maps = [];
                $maps[] = ['id','=',$matterid];

                $m_data = [];
                $m_data['status'] = 1;
                $m_data['confirm_status'] = 1;
                $r = model('matter')->force()->save($m_data,$maps);

                //更新参与事项积分
                $si = new SaveIntegral();
                $r = $si->save_jm_integral($matterid);

                //发送消息
                $r = $this->send_matter_msg($matterid,101,'已解决');
                break;
            case 102:
                $maps = [];
                $maps[] = ['id','=',$matterid];
                $m_detail = model('matter')->get_detail($maps,'id,from_openid');

                $m_data = [];
                $m_data['deal_openid'] = $m_detail['from_openid'];
                $m_data['out_time'] = date('Y-m-d H:i:s',strtotime('+12 hour'));
                $m_data['is_read'] = 0;
                $m_data['confirm_status'] = 0;
                $r = model('matter')->force()->save($m_data,$maps);

                //发送消息
                $r = $this->send_matter_msg($matterid,102,'已解决');
                break;
            case 103:
                $maps = [];
                $maps[] = ['id','=',$matterid];

                $m_data = [];
                $m_data['out_time'] = $out_time;
                $m_data['is_read'] = 1;
                $m_data['confirm_status'] = 1;
                $r = model('matter')->force()->save($m_data,$maps);

                //发送消息
                $r = $this->send_matter_msg($matterid,103,'稍后处理');
                break;
            case 104:
                $maps = [];
                $maps[] = ['id','=',$matterid];

                $m_data = [];
                $m_data['deal_openid'] = $deal_openid;
                $m_data['out_time'] = date('Y-m-d H:i:s',strtotime('+12 hour'));
                $m_data['is_read'] = 0;
                $m_data['confirm_status'] = 0;
                $r = model('matter')->force()->save($m_data,$maps);

                //发送消息
                $r = $this->send_matter_msg($matterid,104,'移交');
                break;
            case 105:
                $maps = [];
                $maps[] = ['matterid','=',$matterid];
                $maps[] = ['user_openid','<>',$user_openid];
                $mf_detail = model('matter_flow')->get_detail($maps,'id,user_openid','id desc');

                $maps = [];
                $maps[] = ['id','=',$matterid];

                $m_data = [];
                $m_data['deal_openid'] = $mf_detail['user_openid'];
                $m_data['out_time'] = date('Y-m-d H:i:s',strtotime('+12 hour'));
                $m_data['is_read'] = 0;
                $m_data['confirm_status'] = 0;
                $r = model('matter')->force()->save($m_data,$maps);

                //发送消息
                $r = $this->send_matter_msg($matterid,105,'未解决');
                break;
            default:
                return 0;
        }
        return 1;
    }

    /**
     * 发送事项消息
     * @param $matterid
     * @param $action
     * @param $remark
     * @return int
     */
    public function send_matter_msg($matterid,$action,$remark){
        $maps = [];
        $maps[] = ['id','=',$matterid];
        $m_detail = model('matter')->get_detail($maps,'id,from_openid,name,deal_openid');

        //查找最后一个处理人用户
        $maps = [];
        $maps[] = ['matterid','=',$m_detail['id']];
        $mf_detail = model('matter_flow')->get_detail($maps,'id,user_openid','id desc');
        if(!empty($mf_detail)){
            $maps = [];
            $maps[] = ['openid','=',$mf_detail['user_openid']];
            $last_u_detail = model('user')->get_detail($maps,'id,username,tel');
        }

        //发送消息
        $sm = new SendMsg();
        if($action == 101){
            $maps = [];
            $maps[] = ['matterid','=',$m_detail['id']];
            $user_openid_arr = model('matter_flow')->get_column($maps,'DISTINCT user_openid');
            foreach($user_openid_arr as $k => $v){
                $r = $sm->send_matter_msg($v,$m_detail['name'],$last_u_detail['username'],$last_u_detail['tel'],$remark);
            }
        }elseif($action == 102){
            $r = $sm->send_matter_msg($m_detail['deal_openid'],$m_detail['name'],$last_u_detail['username'],$last_u_detail['tel'],$remark);
        }elseif($action == 103){
            $r = $sm->send_matter_msg($m_detail['from_openid'],$m_detail['name'],$last_u_detail['username'],$last_u_detail['tel'],$remark);
        }elseif($action == 104){
            $r = $sm->send_matter_msg($m_detail['deal_openid'],$m_detail['name'],$last_u_detail['username'],$last_u_detail['tel'],$remark);
        }elseif($action == 105){
            $r = $sm->send_matter_msg($m_detail['deal_openid'],$m_detail['name'],$last_u_detail['username'],$last_u_detail['tel'],$remark);
        }else{
            //查找发起人用户
            $maps = [];
            $maps[] = ['openid','=',$m_detail['from_openid']];
            $from_u_detail = model('user')->get_detail($maps,'id,username,tel');
            $r = $sm->send_matter_msg($m_detail['deal_openid'],$m_detail['name'],$from_u_detail['username'],$from_u_detail['tel'],$remark);
        }
        return 1;
    }
}